package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.PointDao;
import cn.topevery.garden.entity.po.Point;
import cn.topevery.garden.entity.query.PointQuery;
import cn.topevery.garden.entity.vo.PointVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Repository
public class PointDaoImpl extends BaseDaoImpl<Point> implements PointDao {
    @Override
    public <P extends DbCsp> List<Point> getSearch(P para) {
        return null;
    }

    @Override
    public DataPager<PointVO> listPage(PointQuery query) {
        String table = "t_point tp ";
        String columns = "tp.*, ( select count(1) from t_person t where tp.c_point_id=t.c_point_id and t.c_call_status=1) personCount, " +
                "( select count(1) from t_equipment t where tp.c_point_id=t.c_point_id and t.c_call_status=1) equipmentCount";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getDistractId()), "c_distract_id", ScriptConditionEnum.Eq, query.getDistractId())
                .where(StringUtils.isNotBlank(query.getRoadId()), "c_road_id", ScriptConditionEnum.Eq, query.getRoadId())
                .where(StringUtils.isNotBlank(query.getSectionId()), "c_section_id", ScriptConditionEnum.Eq, query.getSectionId())
                .where(StringUtils.isNotBlank(query.getPointId()), "c_point_id", ScriptConditionEnum.Like, query.getPointId());
        return getDataPager(sqlQueryMate, pageSize, pageIndex, PointVO.class);
    }

    @Override
    public void deleteBatchById(String userId, Integer[] ids) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_point tp");
        sqlUpdateMate.setColumnValue(true, "tp.c_db_status", 1)
                .setColumnValue(true, "c_db_last_update_date", LocalDateTime.now())
                .setColumnValue(true, "c_db_last_update_id", Integer.valueOf(userId))
                .where(true, "c_id", ScriptConditionEnum.In, ids);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    //获取标段可调入/可调出的样点
    @Override
    public DataPager<PointVO> getPointListBySectionId(PointQuery query) {
        String table = "t_point tp LEFT JOIN t_sys_user tsu on tp.c_db_last_update_id=tsu.c_id";
        String columns = "tp.*,tsu.c_user_name updateName";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getSectionId()), "tp.c_section_id", ScriptConditionEnum.Eq, query.getSectionId())
                .where(query.getStatus()!=null, "tp.c_status", ScriptConditionEnum.Eq, query.getStatus());
        return getDataPager(sqlQueryMate, pageSize, pageIndex, PointVO.class);
    }

    @Override
    public void setPointStatus(List<Integer> idList, Integer status) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_point tp");
        sqlUpdateMate.setColumnValue(true, "tp.c_status", status)
                .where(true, "tp.c_id", ScriptConditionEnum.In, idList);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public List<Map<String, Object>> getListBySectionId(Integer sectionId) {
        String sql = "SELECT tp.c_id id, tp.c_point_id pointId,tp.c_point_num pointNum from t_point tp WHERE c_section_id=" +
                "(SELECT ts.c_section_id FROM t_section ts WHERE c_id=" + sectionId + ")";
        return getMap(sql);
    }

    public DataPager<PointVO> getBySectionId(PointQuery query) {
        String table = "t_point tp";
        String columns = "tp.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getSectionId()!=null, "c_section_id", ScriptConditionEnum.Eq, query.getSectionId());
        return getDataPager(sqlQueryMate, pageSize, pageIndex, PointVO.class);
    }
}
